package com.zhangfb95.crosschain.plugin.container.java.fabric2.infra.data.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhangfb95.crosschain.plugin.container.java.fabric2.infra.model.mapper.entity.Fabric2TxEvent;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author zhangfubing
 * @since 2024/1/9
 */
@Mapper
public interface Fabric2TxEventMapper extends BaseMapper<Fabric2TxEvent> {

    int insertIgnore(@Param("event") Fabric2TxEvent fabric2TxEvent);

    void success(@Param("id") Long id);

    void fail(@Param("id") Long id);

    @Select("""
            SELECT * FROM fabric2_tx_event
             WHERE chain_name = #{chainName}
               AND channel_name = #{channelName}
               AND handle_Status IN ( 'wait', 'fail' ) 
               AND id > #{lastId}
               AND handle_count < 5 
             ORDER BY id ASC LIMIT 0, 10
            """)
    List<Fabric2TxEvent> getWaitOrFailList(@Param("chainName") String chainName,
                                           @Param("channelName") String channelName,
                                           @Param("lastId") long lastId);

    @Select("""
            SELECT * FROM fabric2_tx_event
             WHERE chain_name = #{chainName}
               AND channel_name = #{channelName}
               AND tx_id = #{txId}
            """)
    Fabric2TxEvent getByTxId(@Param("chainName") String chainName,
                             @Param("channelName") String channelName,
                             @Param("txId") String txId);
}
